/*
 * Created on Feb 13, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.compare;

import java.util.Locale;
import java.util.ResourceBundle;

import edu.mit.simile.longwell.Profile;

/**
 * @author dfhuynh
 */
abstract public class ComparatorBase implements IComparator {
	final protected Profile m_profile;
	final protected String	m_parameter;
	
	protected ComparatorBase(Profile profile, String parameter) {
		m_profile = profile;
		m_parameter = parameter;
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.compare.IComparator#compare(java.lang.Object, java.lang.Object, java.lang.String, boolean)
	 */
	public int compare(
		Object value1, Object value2, String locale, boolean ascending) {

		return (ascending ? 1 : -1) * internalCompareAscending(value1, value2, locale);
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.compare.IComparator#getParameter()
	 */
	public String getParameter() {
		return m_parameter;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.compare.IComparator#getAscendingLabel(java.lang.String)
	 */
	public String getAscendingLabel(String locale) {
		return getResources(locale).getString("AscendingLabel");
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.compare.IComparator#getDescendingLabel(java.lang.String)
	 */
	public String getDescendingLabel(String locale) {
		return getResources(locale).getString("DescendingLabel");
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.compare.IComparator#isAscendingByDefault(java.lang.String)
	 */
	public boolean isAscendingByDefault(String locale) {
		return "true".equals(getResources(locale).getString("AscendingByDefault"));
	}

	abstract protected int internalCompareAscending(
		Object v1, Object v2, String locale);
	
	protected ResourceBundle getResources(String locale) {
		return ResourceBundle.getBundle(
				this.getClass().getName(),
				getLocale(locale));
	}
	
	protected Locale getLocale(String locale) {
		return locale == null ? Locale.getDefault() : new Locale(locale);
	}
}
